package com.TP.www.servlet;

import com.TP.www.po.Order;
import com.TP.www.service.OrderPort;
import com.TP.www.service.impl.CustomerAppraise;
import com.TP.www.service.impl.OrderImpl;

import java.util.ArrayList;
import java.util.List;

import static com.TP.www.constant.Constant.*;


/**
 * @author 邱彦祥
 */
public class OrdersMenu {
    /**
     * 顾客展示订单界面
     */
    public static void showOrdersForCustomer(){
        OrderImpl orderImpl = new OrderImpl();
        while(true){
            List<Order> orders = OrderPort.getOrderById(GET_ORDERS_FOR_CUSTOMER_ID,loginUser.getId());
            List<Order> unOrders = new ArrayList<>();
            for (Order order: orders) {
                if(order.getState() == 0) {
                    unOrders.add(order);
                }
            }
            int i ;
            int choose;
            System.out.println("***********订单界面***********");
            if(orders.isEmpty()){
                System.out.println("暂无订单!");
                System.out.println("按任意键返回...");
                SYS_SC.nextLine();
                return;
            }
            i = showOrdersForCustomer(orders);
            System.out.println("*****************************");
            choose = orderImpl.getChoose(i);
            if(choose != 0){
                if(orderImpl.over(unOrders.get(choose - 1))){
                    System.out.println("收货成功!");
                    new CustomerAppraise().addAppraise(unOrders.get(choose - 1));
                }else {
                    System.out.println("收货失败...");
                }
            }else {
                return;
            }
        }
    }

    /**
     * 商家展示订单界面
     */
    public static void showOrdersForMerchant(){
        List<Order> orders = OrderPort.getOrderById(GET_ORDERS_FOR_MERCHANT_ID,loginUser.getId());
        System.out.println("***********订单界面***********");
        if(orders.isEmpty()){
            System.out.println("暂无订单!");
            System.out.println("按任意键返回...");
            SYS_SC.nextLine();
            return;
        }
        showOrdersForMerchant(orders);
        System.out.println("*****************************");
        System.out.println("按 1 以一键接单!(其他键返回上一级)");
        String choose = SYS_SC.nextLine();
        String yi = "1";
        if (yi.equals(choose)){
            if(new OrderImpl().take()){
                System.out.println("接单完成!");
            }else {
                System.out.println("接单失败!");
            }
        }
    }

    /**
     * 将订单按指定格式展示给顾客
     * @param orders 订单
     * @return 商家已接订单数
     */
    private static int showOrdersForCustomer(List<Order> orders){
        List<Order> placeOrders = new ArrayList<>();
        List<Order> unOrders = new ArrayList<>();
        List<Order> overOrders = new ArrayList<>();
        int i = 1;
        for (Order order: orders) {
            if(order.getState() == -1){
                placeOrders.add(order);
            }else if(order.getState() == 0){
                unOrders.add(order);
            }else if (order.getState() == 1){
                overOrders.add(order);
            }
        }
        if (!placeOrders.isEmpty()){
            System.out.println("已下订单");
            for (Order order: placeOrders) {
                System.out.println(order);
            }
        }
        if (!unOrders.isEmpty()){
            System.out.println("商家已接订单");
            for (Order order: unOrders) {
                System.out.println( i + " " + order.toString());
                i++;
            }
        }
        if (!overOrders.isEmpty()){
            System.out.println("已完成订单");
            for (Order order: overOrders) {
                System.out.println(order);
            }
        }
        return i;
    }

    /**
     * 将订单按指定格式展示给商家
     * @param orders 订单
     */
    private static void showOrdersForMerchant(List<Order> orders){
        OrderImpl orderImpl = new OrderImpl();
        List<Order> placeOrders = new ArrayList<>();
        List<Order> unOrders = new ArrayList<>();
        List<Order> overOrders = new ArrayList<>();
        for (Order order: orders) {
            if(order.getState() == -1){
                placeOrders.add(order);
            }else if(order.getState() == 0){
                unOrders.add(order);
            }else if (order.getState() == 1){
                overOrders.add(order);
            }
        }
        if (!placeOrders.isEmpty()){
            System.out.println("已下订单");
            for (Order order: placeOrders) {
                System.out.println(orderImpl.printOrder(order));
            }
        }
        if (!unOrders.isEmpty()){
            System.out.println("商家已接订单");
            for (Order order: unOrders) {
                System.out.println(orderImpl.printOrder(order));
            }
        }
        if (!overOrders.isEmpty()){
            System.out.println("已完成订单");
            for (Order order: overOrders) {
                System.out.println(orderImpl.printOrder(order));
            }
        }
    }
}
